﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Text;
using System.Security.Cryptography;
using System.IO;

using CE.Global;
using CE.Database;
using CE.Configuration;

public partial class savings_life_insurance : MyLanguageBasePage
{
    HtmlMeta language = new HtmlMeta();
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    string selectedLanguage;
    protected void Page_Load(object sender, EventArgs e)
    {
         string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.IsCXLive");
         if (_strCXLive == "0")
         {
             if (Session[CE.Global.General.Guest] == null)
             {
                 Response.Redirect("~/Default");
             }

         }

         LiteralControl newln = new LiteralControl(Environment.NewLine);
         language.Name = "language";
         language.Content = Resources.Title.PageLanguage;
         HtmlMeta keywords = new HtmlMeta();
         keywords.Name = "keywords";
         keywords.Content = Resources.FWD.MetaKeyword;
         HtmlMeta description = new HtmlMeta();
         description.Name = "description";
         description.Content = Resources.FWD.MetaDescription;

         Page.Header.Controls.AddAt(10, new LiteralControl(Environment.NewLine));
         Page.Header.Controls.AddAt(11, language);
         Page.Header.Controls.AddAt(12, new LiteralControl(Environment.NewLine));
         Page.Header.Controls.AddAt(13, keywords);
         Page.Header.Controls.AddAt(14, new LiteralControl(Environment.NewLine));
         Page.Header.Controls.AddAt(15, description);

         Page.Header.Title = Resources.FWD.PageTitle;

         HtmlLink css = new HtmlLink();
         css.Href = Resources.FWD.CssSavingsUrl;
         css.Attributes["rel"] = "stylesheet";
         css.Attributes["type"] = "text/css";
         css.Attributes["media"] = "all";
         Page.Header.Controls.Add(css);

        if (!Page.IsPostBack)
        {
            LoadFWDAmount();
            ibtnLanguage.ImageUrl = Resources.FWD.ImgLanguageUrl;
            hplHome.NavigateUrl = CE.Global.General.GetUrl("/", Page);
            ddlAmount.Attributes.Add("onblur", "ShowValidationEffect('" + ddlAmount.ClientID + "','" + rfvAmount.ClientID + "');");

        }
    }

    protected void ibtnLanguage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        selectedLanguage = string.Empty;

        if (Session["Language"] != null)
        {
            selectedLanguage = Session["Language"].ToString();
        }
        else
        {
            selectedLanguage = "th-TH";
            Session["Language"] = selectedLanguage;
        }

        if (selectedLanguage == "th-TH")
        {
            Session["Language"] = "en-US";
        }
        else
        {
            Session["Language"] = "th-TH";
        }

        hdnLanguage.Value = selectedLanguage;
        if (selectedLanguage == "en-US")
        {
            Response.Redirect(Request.UrlReferrer.AbsoluteUri.Replace("/en", ""));
        }
        else
        {
            string[] _arrList = Request.UrlReferrer.AbsoluteUri.Split('/');
            string _redirectUrl = "";
            _arrList[3] = "en/" + _arrList[3];
            for (int i = 0; i < _arrList.Length; i++)
            {

                if (i != _arrList.Length - 1)
                {
                    _redirectUrl += _arrList[i].ToString() + "/";
                }
                else
                {
                    _redirectUrl += _arrList[i].ToString();
                }

            }
            if (_arrList.Length == 4 && _redirectUrl.EndsWith("/"))
            {
                _redirectUrl += "home.aspx";
            }

            Response.Redirect(_redirectUrl);
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("life");
        if (Page.IsValid)
        {
            string _redirectUrl = "";
            _redirectUrl = CE.Global.General.GetUrl("~/savings/result", Page);
            Response.Redirect(_redirectUrl + "?val=" + ddlAmount.Text + "&fwd=yes");
        }
    }

    public void LoadFWDAmount()
    {
        try
        {

            MyDatabase _db = new MyDatabase();
            DataTable _dtblFWDAmount = new DataTable();
            SortedList _spList = new SortedList();
            _dtblFWDAmount = _db.GetDataTable("sprocFWDPremiumSelectedItem");
            if (_dtblFWDAmount != null && _dtblFWDAmount.Rows.Count > 0)
            {
                ddlAmount.Items.Clear();
                ddlAmount.DataSource = _dtblFWDAmount;
                ddlAmount.DataTextField = "AnnualSavingsDisp";
                ddlAmount.DataValueField = "FWDId";
                ddlAmount.DataBind();
                ddlAmount.Items.Insert(0, new ListItem(Resources.FWD.SelectAmount, "-1"));
            }
            else
            {
                ddlAmount.Items.Clear();
                ddlAmount.Items.Insert(0, new ListItem(Resources.FWD.SelectAmount, "-1"));
            }
        }
        catch (Exception ex)
        {
        }
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvAmount.Enabled && rfvAmount.ValidationGroup == strValidationGroup)
        {
            rfvAmount.Validate();
            if (rfvAmount.IsValid == false)
            {
                ddlAmount.ControlStyle.CssClass = "contactDropdown validationError";
            }
            else
            {
                ddlAmount.ControlStyle.CssClass = "contactDropdown";
            }
        }
        else
        {
            ddlAmount.ControlStyle.CssClass = "contactDropdown";
        }
    }

}
